var DBFactory = require('../conn/DBFactory');
var boutiquePoolDao = module.exports;

function gatherGoods(gatherSql, i, cb) {
    DBFactory.getConnection(function(error, connection) {
        if(error){
            console.log(error);
        }else {
            var sql = 'replace into boutique_pool(`pictUrl`,`shopTitle`,`title`,`tkRate`,`tkCommFee`,`hiddenStartTime`,' +
                '`hiddenEndTime`,`biz30day`,`zkPrice`,`auctionUrl`,`hiddenAmount`,`hiddenStartFee`,`hiddenInfo`,' +
                '`tag`,`auctionId`,`match_content`,`adTitle`,`sellerId`,`includeDxjh`,`userType`,`hiddenActivityId`,`pool_type`,`mp4Url`) ' +
                'select `pictUrl`,`shopTitle`,`title`,`tkRate`,`tkCommFee`,`hiddenStartTime`,`hiddenEndTime`,`biz30day`,`zkPrice`,' +
                '`auctionUrl`,`hiddenAmount`,`hiddenStartFee`,`hiddenInfo`,`tag`,`auctionId`,`match_content`,`adTitle`,`sellerId`,' +
                '`includeDxjh`,`userType`, `hiddenActivityId`, ? , `mp4Url` from goods where id in ( select a.id from (' + gatherSql[i].sql +') as a )';
            connection.query(sql, [gatherSql[i].pool_type], function(err, result) {
                if(err) {
                    console.log('gatherGoods err:' + err);
                    connection.release();
                    return;
                } else {
                    console.log('插入'+ result.affectedRows + '条成功！');
                    ++i;
                    if(i <= gatherSql.length -1) {
                        gatherGoods(gatherSql, i, cb);
                    } else {
                        cb(null, '采集完成！');
                    }
                    connection.release();
                    return;
                }
            });
        }
    });
}

function getPoolGoodsSql(cb) {
    var sqls = [];
    //爆款：      select id from goods where biz30day > 15000 and tkCommFee > 10 and zkPrice < 180 ORDER BY RAND() limit 50;
    //实惠：      select id from goods where zkPrice < 20 and hiddenAmount> 9 ORDER BY RAND() limit 50;
    //高佣：      select id from goods where isHighCoupon = 1  ORDER BY RAND() limit 50;
    //9.9:       select id from goods where zkPrice <= 9.9 and includeDxjh = 1 ORDER BY RAND() limit 50;
    //超值好物    select id from goods where zkPrice >= 9 and zkPrice <= 50 and biz30day > 1500 and tkRate >= 20 ORDER BY RAND() limit 50;
    //超级人气    select id from goods where biz30day > 3500  ORDER BY RAND() limit 50;
    //母婴        select id from goods where tag = 母婴  ORDER BY RAND() limit 50;
    //聚划算      select id from goods where activityType = 聚划算  ORDER BY RAND() limit 50;
    //生活家      select id from goods where tag = 居家  ORDER BY RAND() limit 50;
    //美食        select id from goods where tag = 美食  ORDER BY RAND() limit 50;
    //鞋包服      select id from goods where tag = 男装 or  tag = 女装 or tag = 鞋品 ORDER BY RAND() limit 150;
    // sqls.push({sql:'select id from goods where biz30day > 15000 and tkCommFee > 10 and zkPrice < 180 ORDER BY RAND() limit 100', pool_type:0});
    sqls.push({sql:'select id from goods where zkPrice < 20 and hiddenAmount> 9 ORDER BY RAND() limit 100', pool_type:2});
    sqls.push({sql:'select id from goods  where isHighCoupon = 1  ORDER BY RAND() limit 100', pool_type:3});
    sqls.push({sql:'select id from goods  where zkPrice < 9.9 and includeDxjh = 1 ORDER BY RAND() limit 100', pool_type:5});
    sqls.push({sql:"select id from goods where pdId > 0 limit 100 ", pool_type:8});
    sqls.push({sql:'select id from goods where biz30day > 3500  ORDER BY RAND() limit 50', pool_type:6});
    sqls.push({sql:"select id from goods where tag = '母婴'  ORDER BY RAND() limit 50", pool_type:10});
    sqls.push({sql:"select id from goods where activityType = '聚划算' ORDER BY RAND() limit 50", pool_type:11});
    sqls.push({sql:"select id from goods where tag = '居家'  ORDER BY RAND() limit 50", pool_type:12});
    sqls.push({sql:"select id from goods where tag = '美食'  ORDER BY RAND() limit 50", pool_type:13});
    sqls.push({sql:"select id from goods where tag = '男装' or  tag = '女装' or tag = '鞋品' ORDER BY RAND() limit 150", pool_type:15});
    // sqls.push({sql:"select id from goods where zkPrice-hiddenAmount <= 50 and realTkRate>=30 order by biz30day desc limit 100", pool_type:17});
    gatherGoods(sqls, 0, cb)
}

boutiquePoolDao.sysGather = function(cb) {
    getPoolGoodsSql(cb)
}


function emptySomePool(cb) {
    DBFactory.getConnection(function(error, connection) {
        if(error){
            console.log(error);
        }else {
            var sql = 'DELETE from boutique_pool where pool_type in (2,3,5,8)';
            connection.query(sql, [], function(err) {
                if(err) {
                    console.log('selectedToPool err:' + err);
                    connection.release();
                    return;
                } else {
                    connection.release();
                    getPoolGoodsSql(cb);
                }
            });
        }
    });
}
boutiquePoolDao.sysGatherEveryDay = function(cb) {
    emptySomePool(cb);
}

boutiquePoolDao.delExpiredGoods = function(cb) {
    DBFactory.getConnection(function(error, connection) {
        if(error){
            console.log(error);
        }else {
            var sql = 'delete from boutique_pool where auctionId not in( select auctionId from goods )';
            connection.query(sql, [], function(err) {
                if(err) {
                    console.log('delExpiredGoods err:' + err);
                    connection.release();
                    return;
                } else {
                    connection.release();
                    // getPoolGoodsSql(cb);
                }
            });
        }
    });
}